package com.celesea.framework.entity;

import com.celesea.framework.authority.User;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import java.util.Calendar;
import java.util.Date;

/**
 * ${DESCRIPTION}
 *
 * @author JackYuan
 * @create 2017-10-25 14:13
 */
public abstract class BusEntity extends BaseEntity {

    public static final String TALENT_ID = "tenantId";
    public static final String DELETE_FLAG = "deleteFlag";
    public static final String VERSION = "version";

    /**
     * 服务器创建时间.
     */
    @Column(name = "`server_create_time`", columnDefinition = "创建时间", nullable = false, updatable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date serverCreateTime;

    /**
     * 创建者ID.
     */
    @Column(name = "`creator_id`", columnDefinition = "创建者ID", nullable = false, updatable = false)
    protected String creatorId;

    /**
     * 创建者姓名.
     */
    @Column(name = "`creator_name`", columnDefinition = "创建者姓名", nullable = false, updatable = false)
    protected String creatorName;

    /**
     * 服务器最后修改时间.
     */
    @Column(name = "`server_update_time`", columnDefinition = "最后修改时间", nullable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected Date serverUpdateTime;

    /**
     * 最后修改者ID.
     */
    @Column(name = "`updator_id`", columnDefinition = "最后修改者ID", nullable = false)
    protected String updatorId;

    /**
     * 最后修改者姓名.
     */
    @Column(name = "`updator_name`", columnDefinition = "最后修改者姓名", nullable = false)
    protected String updatorName;

    /**
     * 逻辑删除状态.
     */
    @Column(name = "`delete_flag`", columnDefinition = "逻辑删除状态:[0：删除，1：正常]", nullable = false)
    protected Byte deleteFlag;

    /**
     * 数据版本控制
     */
    @Column(name = "`version`", columnDefinition = "数据版本控制", nullable = false)
    protected Long version;


    public BusEntity() {
    }

    public BusEntity delete() {
        this.deleteFlag = Const.DeleteFlag.DELETE;
        return this;
    }

    public BusEntity insertInfo() {
        User user = EnvUtil.getLoginUser();
        this.tenantId = EnvUtil.getTenantId();
        this.creatorId = user.getId();
        this.creatorName = user.getName();
        this.serverCreateTime = Calendar.getInstance().getTime();
        this.deleteFlag = Const.DeleteFlag.NORMAL;
        this.version = 0L;
        updateInfo();
        return this;
    }

    public BusEntity updateInfo() {
        User user = EnvUtil.getLoginUser();
        this.tenantId = EnvUtil.getTenantId();
        this.updatorId = user.getId();
        this.updatorName = user.getName();
        this.serverUpdateTime = Calendar.getInstance().getTime();
        this.deleteFlag = Const.DeleteFlag.NORMAL;
        if (this.version == null) {
            this.version = 0L;
        }
        return this;
    }

    public void incrementVersion() {
        Long version = this.version == null ? 0L : this.version;
        this.version = version + 1;
    }

    public Date getServerCreateTime() {
        return serverCreateTime;
    }

    public void setServerCreateTime(Date serverCreateTime) {
        this.serverCreateTime = serverCreateTime;
    }

    public Date getServerUpdateTime() {
        return serverUpdateTime;
    }

    public void setServerUpdateTime(Date serverUpdateTime) {
        this.serverUpdateTime = serverUpdateTime;
    }

    public String getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public String getUpdatorId() {
        return updatorId;
    }

    public void setUpdatorId(String updatorId) {
        this.updatorId = updatorId;
    }

    public String getUpdatorName() {
        return updatorName;
    }

    public void setUpdatorName(String updatorName) {
        this.updatorName = updatorName;
    }

    public Byte getDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(Byte deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }
}
